<?
class cxc_perito{

	// Propiedades
	var $id;
	var $id_perito;
	var $codigo_perito;
	var $nombre_perito;
	var $id_zona;
	var $codigo_zona;
	var $nombre_zona;
	var $id_concepto;
	var $nombre_concepto;
	var $descripcion;
	var $fecha;
	var $cantidad;
	var $monto;
	var $estatus;


	var $total;

	function get($conn, $id){
		try {
			$q = "SELECT A.*, B.id_zona, B.codigo AS codigo_perito, B.nombre AS nombre_perito, D.codigo AS codigo_zona, D.nombre AS nombre_zona, E.nombre AS nombre_concepto FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id  WHERE A.id= $id";
			//die($q);
			$r = $conn->Execute(utf8_encode($q));
			if(!$r->EOF){
				$this->id = $r->fields['id'];
				$this->id_perito = $r->fields['id_perito'];
				$this->codigo_perito = $r->fields['codigo_perito'];
				$this->nombre_perito = $r->fields['nombre_perito'];
				$this->id_zona = $r->fields['id_zona'];
				$this->codigo_zona = $r->fields['codigo_zona'];
				$this->nombre_zona = $r->fields['nombre_zona'];
				$this->id_concepto = $r->fields['id_concepto'];
				$this->nombre_concepto = $r->fields['nombre_concepto'];
				$this->descripcion = $r->fields['descripcion'];
				$this->fecha = $r->fields['fecha'];
				$this->cantidad = $r->fields['cantidad'];
				$this->monto = $r->fields['monto'];
				switch ($r->fields['estatus']) {
					case "P":
						$this->estatus = "PENDIENTE";
						break;
					case "C":
						$this->estatus = "CANCELADA";
						break;
				}

			}


		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function get_all($conn, $orden="id DESC", $Tipo, $Valor, $TipoE, $max=10, $from=1){
		try {

			$TipoE = empty($TipoE) ? 0 : $TipoE;
			
			if(empty($Valor)){
				$q = "SELECT id FROM cxc_peritos ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==0){
				$q = "SELECT A.id FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
				$q.= "WHERE D.nombre::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			//	die($q);			

			}
			elseif($Tipo==1){
				$q = "SELECT A.id FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
				$q.= "WHERE B.nombre::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==2){
				$q = "SELECT A.id FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
				$q.= "WHERE E.nombre::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}

			elseif($Tipo==3){
				$q = "SELECT A.id FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
				$q.= "WHERE A.fecha::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==4){
				$q = "SELECT A.id FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
				$q.= "WHERE A.monto::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==5){
				$q = "SELECT A.id FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
				$q.= "WHERE D.codigo::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			//	die($q);			
			}
			elseif($Tipo==6){
				$q = "SELECT A.id FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
				$q.= "WHERE B.codigo::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			//die($q);			
			$r = ($max!=0) ? $conn->SelectLimit($q, $max, $from) : $conn->Execute(utf8_encode($q));
			$coleccion=array();
			while(!$r->EOF){
				$ue = new cxc_perito;
				$ue->get($conn, $r->fields['id']);
				$coleccion[] = $ue;
				$r->movenext();
			}
			$this->total = $r->RecordCount();
			return $coleccion;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}


	function add($conn, $id_perito, $id_concepto, $descripcion, $fecha, $monto, $cantidad){
		try {
			$q = "INSERT INTO cxc_peritos ";
			$q.= "(id_perito, id_concepto, descripcion, fecha, monto, cantidad) ";
			$q.= "VALUES ";
			$q.= "(NULLIF($id_perito,-1), NULLIF($id_concepto,-1), upper('$descripcion'), NULLIF('$fecha','')::date, COALESCE(NULLIF('$monto',''),'0')::numeric, COALESCE(NULLIF('$cantidad',''),'1')::numeric)";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);

			return REG_ADD_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function set($conn, $id, $id_perito, $id_concepto, $descripcion, $fecha, $monto, $cantidad){
		try {
			$q = "UPDATE cxc_peritos SET id_perito= NULLIF($id_perito,-1), id_concepto= NULLIF($id_concepto,-1), descripcion = upper('$descripcion'), fecha = NULLIF('$fecha','')::date, monto = COALESCE(NULLIF('$monto',''),'0')::numeric, cantidad = COALESCE(NULLIF('$cantidad',''),'1')::numeric ";
			$q.= " WHERE id=$id";	
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_SET_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}


	function del($conn, $id){
		try {
			$q = "DELETE FROM cxc_peritos WHERE id=$id";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_DEL_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function total_registro_busqueda($conn,$Tipo, $Valor, $TipoE, $orden="id")
	{
		$TipoE = empty($TipoE) ? 0 : $TipoE;
		if(empty($Valor)){
			$q = "SELECT count(*) AS total FROM cxc_peritos";
		}
		elseif($Tipo==0){
			$q = "SELECT count(*) AS total FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id  ";
			$q.= " WHERE D.nombre::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==1){
			$q = "SELECT count(*) AS total FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
			$q.= " WHERE B.nombre::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==2){
			$q = "SELECT count(*) AS total FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
			$q.= " WHERE E.nombre::text ILIKE '%$Valor%'::text";
		}

		elseif($Tipo==3){
			$q = "SELECT count(*) AS total FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
			$q.= " WHERE A.fecha::text ILIKE '%$Valor%'::text";
		}

		elseif($Tipo==4){
			$q = "SELECT count(*) AS total FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
			$q.= " WHERE A.monto::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==5){
			$q = "SELECT count(*) AS total FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id  ";
			$q.= " WHERE D.codigo::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==6){
			$q = "SELECT count(*) AS total FROM  cxc_peritos AS A INNER JOIN peritos AS B ON A.id_perito = B.id INNER JOIN zonas AS D ON B.id_zona = D.id INNER JOIN conceptos AS E ON A.id_concepto = E.id ";
			$q.= " WHERE B.codigo::text ILIKE '%$Valor%'::text";
		}

		$r = $conn->Execute(utf8_encode($q));
		$total = $r->fields['total'];
		return $total;
	}
	
	function auditoria($conn, $query) {
		$id_usuario = $_SESSION['id'];
		$q = "INSERT INTO auditoria(id_usuario, query) VALUES($id_usuario,$$" . $query . "$$);";
		//die($q);
		$conn->Execute(utf8_encode($q));

		return true;
	}
}
?>
